package com.bluepj.led2;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.hardware.Camera.CameraInfo;
import android.graphics.SurfaceTexture;
import android.view.Surface;
import java.io.IOException;

import java.util.List;

public class MainActivity extends Activity {

    private boolean isFlashOn = false;
    private Camera camera;
    private Button button;

    public static SurfaceView preview;
    public static SurfaceHolder mHolder;

    int cameraId = 0;


    @Override
    protected void onStop() {
        super.onStop();

        if (camera != null) {
            camera.release();
        }
    }

    private int findBackFacingCamera() {
        int cameraId = -1;
        String NumCam="";
        // Search for the back facing camera
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraInfo.CAMERA_FACING_BACK) {

                cameraId = i;
                NumCam=String.valueOf(i);
                Log.i("info", "############################ Camera found: #"+NumCam);
                break;
            }
        }
        return cameraId;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.buttonFlashlight);


        Context context = this;
        PackageManager pm = context.getPackageManager();


        // do we have a camera?
        if (!getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
                    .show();
        } else {
            cameraId = findBackFacingCamera();
            if (cameraId < 0) {
                Toast.makeText(this, "No back facing camera found.",
                        Toast.LENGTH_LONG).show();
            }
        }

        camera = Camera.open(cameraId);

        final Parameters p = camera.getParameters();

        String modef = p.getFlashMode();

        List flashModes = p.getSupportedFlashModes();
        Log.i("info", "Flash modes: " + flashModes);
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(p);
        isFlashOn = true;
        camera.startPreview();


           button.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                if (isFlashOn) {
                    Log.i("info", "torch is turned off!");
                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(p);
                    //camera.stopPreview();
                    isFlashOn = false;
                    button.setText("Torch-ON");
                } else {
                    Log.i("info", "torch is turned on!");
                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    camera.setParameters(p);
                    //camera.startPreview();
                    isFlashOn = true;
                    button.setText("Torch-OFF");
                }
            }
        });

    }
}

